//
//  BCCoinListController.swift
//  Blockfolio
//
//  Created by Maybe on 09/05/2021.
//

import UIKit
import HandyJSON
import ESPullToRefresh

class BCCoinListController: BCBaseController {
    
    private let BCCoinListCellID = "BCCoinListCellID"
    fileprivate var dataList  = [BCCoinModel?]()
    
    fileprivate var page = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
//        loadData()
        
        // Do any additional setup after loading the view.
        
        self.tableView.es.addPullToRefresh {
            self.page = 1
            self.loadData()
        }
        
        self.tableView.es.addInfiniteScrolling {
//            [self] in
            self.page += 1
            self.loadData()
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        loadData()
    }
    
    fileprivate lazy var tableView: UITableView = {
        let tableview = UITableView()
        tableview.backgroundColor = UIColor.hex("#f5f7f9")
        tableview.tableFooterView = UIView()
        tableview.dataSource = self
        tableview.delegate = self
        tableview.separatorStyle = .none
        tableview.register(BCCoinListCell.classForCoder(), forCellReuseIdentifier: BCCoinListCellID)
        return tableview
    }()
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

//MARK: - create UI
extension BCCoinListController{
    fileprivate func setupUI(){
//        title = "交易对"
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
    }
}



// MARK: - UITableViewDataSource & UITableViewDelegate
extension BCCoinListController : UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        self.dataList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:BCCoinListCell = tableView.dequeueReusableCell(withIdentifier: BCCoinListCellID, for: indexPath) as! BCCoinListCell
        cell.selectionStyle = .none
        cell.coinModel = self.dataList[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 88.0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = BCLineChartController.init()
        navigationController?.pushViewController(vc, animated: true)
    }
    
}

// MARK: - LoadData

extension BCCoinListController {
    fileprivate func loadData(){
        let params:[String : Any] = [
            "vs_currency":"usd",
            "order":"market_cap_desc",
            "per_page":20,
            "page":page,
            "sparkline":0,
        ]
        BCNetworkUtil.shared.BCGet(url: Api.coinMarets, params: params) { (respone) in
            
            guard let list = [BCCoinModel].deserialize(from: respone) else {return}
            if self.page == 1{
                self.dataList = list
            }else {
                self.dataList += list
            }
            self.tableView.reloadData()
            BClog("解析后的数据\(list)")
//            self.tableView.es.stopPullToRefresh(completion: true)
            self.tableView.es.stopPullToRefresh()
            self.tableView.es.stopLoadingMore()
        } error: { (respone) in
            BClog(respone)
            self.tableView.es.stopPullToRefresh()
            self.tableView.es.stopLoadingMore()
        }
  
        
    }
}
